home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_sound.lha / Sound / SND_Init.c < prev    next >
Encoding:
Text File  |  1998-09-19  |  3.9 KB  |  160 lines

  1. /************************************************************************************
  2. ** Action: Get()
  3. ** Object: Sound
  4. */
  5.  
  6. LIBFUNC struct Sound * SND_Get(mreg(__a0) struct Stats *Stats)
  7. {
  8.   struct Sound *Sound;
  9.  
  10.   if (Sound = AllocMemBlock(sizeof(struct Sound), MEM_RESOURCED|Stats->MemFlags)) {
  11.      Sound->Head.ID      = ID_SOUND;
  12.      Sound->Head.Version = VER_SOUND;
  13.      Public->OpenCount++;
  14.      return(Sound);
  15.   }
  16.   else {
  17.      ErrCode(ERR_FAILED);
  18.      return(NULL);
  19.   }
  20. }
  21.  
  22. /************************************************************************************
  23. ** Action: Init()
  24. ** Object: Sound
  25. **
  26. ** Initialises a sound for the play routine.  Currently this function only supports
  27. ** sounds of type IFF, but support for sound files like WAVE should be implemented
  28. ** in child modules anyway.
  29. **
  30. ** To Do
  31. ** -----
  32. ** IFF needs to support setting of period/octave and frequency.
  33. */
  34.  
  35. LIBFUNC LONG SND_Init(mreg(__a0) struct Sound *Sound)
  36. {
  37.   LONG *lngptr, *bodyptr, filesize, read;
  38.   struct File *file = NULL;
  39.   LONG error = ERR_FAILED;
  40.  
  41.   if (SndAlloc IS NULL) {
  42.      if (LIBAllocAudio() IS ERR_OK) {
  43.         DPrintF("Init:","Audio channels allocated.");
  44.         SndAlloc = 1;
  45.      }
  46.      else DPrintF("!Init:","Could not allocate audio channels from the OS.");
  47.   }
  48.  
  49.   if ((Sound->Source) AND (Sound->prvHeader IS NULL)) {
  50.      if (file = Get(ID_FILE|GET_NOTRACK)) {
  51.         file->Source = Sound->Source;
  52.         file->Flags  = FL_READ|FL_OLDFILE|FL_FIND;
  53.  
  54.         if (Init(file, NULL)) {
  55.            if ((filesize = GetFSize(file)) > 0) {
  56.               if (Sound->prvHeader = AllocMemBlock(filesize, MEM_SOUND)) {
  57.                  if ((read = Read(file, Sound->prvHeader, filesize)) != filesize) {
  58.                     DPrintF("!Init:","Error - could only read %ld of %ld bytes.",read,filesize);
  59.                     goto exit;
  60.                  }
  61.               }
  62.               else {
  63.                  ErrCode(ERR_MEMORY);
  64.                  goto exit;
  65.               }
  66.            }
  67.            else {
  68.               DPrintF("!Init:","Could not obtain a file size.");
  69.               goto exit;
  70.            }
  71.         }
  72.         else goto exit;
  73.      }
  74.      else goto exit;
  75.   }
  76.  
  77.   if (lngptr = Sound->prvHeader) {
  78.      if ((lngptr[0] != CODE_FORM) OR (lngptr[2] != CODE_8SVX)) {
  79.         DPrintF("Init:","Sound file identified as being raw data.");
  80.  
  81.         Sound->Length = GetMemSize(Sound->prvHeader);
  82.         if (Sound->Data IS NULL) {
  83.            Sound->Data = Sound->prvHeader;
  84.         }
  85.      }
  86.      else {
  87.         DPrintF("Init:","Processing IFF Sound file.");
  88.  
  89.         if (bodyptr = FindHeader(lngptr, CODE_BODY)) {
  90.            if (Sound->Length IS NULL) {
  91.               Sound->Length = bodyptr[-1];
  92.            }
  93.  
  94.            if (Sound->Data IS NULL) {
  95.               Sound->Data = bodyptr;
  96.            }
  97.         }
  98.         else {
  99.            DPrintF("!Init:","IFF BODY header not found!");
  100.            goto exit;
  101.         }
  102.      }
  103.   }
  104.   else {
  105.      DPrintF("!Init:","No Sound->prvHeader.");
  106.      goto exit;
  107.   }
  108.  
  109.   if (Sound->Octave IS NULL) Sound->Octave = OCT_C2;
  110.   if (Sound->Volume IS NULL) Sound->Volume = 100;
  111.   error = ERR_OK;
  112.  
  113. exit:
  114.   if (file) Free(file);
  115.  
  116.   if (error IS ERR_OK) {
  117.      SoundCount++;      /* Keep a counter of initialised Sounds in the system */
  118.   }
  119.   else {
  120.      FreeMemBlock(Sound->prvHeader);
  121.      Sound->prvHeader = NULL;
  122.   }
  123.  
  124.   return(error);
  125. }
  126.  
  127. /************************************************************************************
  128. ** Action: Load()
  129. ** Object: Sound
  130. */
  131.  
  132. LIBFUNC struct Sound * SND_Load(mreg(__a0) struct File *Source)
  133. {
  134.   return(InitTags(NULL,
  135.     TAGS_SOUND,  NULL,
  136.     SA_Source, Source,
  137.     TAGEND));
  138. }
  139.  
  140. /************************************************************************************
  141. ** Action: Free()
  142. ** Object: Sound
  143. */
  144.  
  145. LIBFUNC void SND_Free(mreg(__a0) struct Sound *Sound)
  146. {
  147.   if (Sound->prvHeader) {
  148.      FreeMemBlock(Sound->prvHeader);
  149.   }
  150.  
  151.   Public->OpenCount--;
  152.   SoundCount--;
  153.  
  154.   if ((SoundCount IS NULL) AND (SndAlloc != NULL)) {
  155.       LIBFreeAudio();
  156.       SndAlloc = NULL;
  157.   }
  158. }
  159.  
  160.